package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.GoodsSuppliers;
import com.dingreading.cloud.admin.entity.WhPurchaseGoods;
import com.dingreading.cloud.admin.entity.table.GoodsSuppliersTableDef;
import com.dingreading.cloud.admin.mapper.GoodsSuppliersMapper;
import com.dingreading.cloud.admin.service.GoodsSuppliersService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.GoodsTableDef.goods;
import static com.dingreading.cloud.admin.entity.table.GoodsTypeTableDef.goodsType;
import static com.dingreading.cloud.admin.entity.table.GoodsWetPriceTableDef.goodsWetPrice;
import static com.dingreading.cloud.admin.entity.table.SuppliersTableDef.suppliers;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsSuppliersServiceImpl extends BaseServiceImpl<GoodsSuppliersMapper, GoodsSuppliers, GoodsSuppliersTableDef> implements GoodsSuppliersService {


    @Override
    protected GoodsSuppliersTableDef getTable() {
        return GoodsSuppliersTableDef.goodsSuppliers;
    }

    @Override
    public Page<GoodsSuppliers> pageList(PageUtil pageUtil, Long supplierId, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (supplierId != null)
            condition.and(table.supplierId.eq(supplierId));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.id, table.supplierId, table.goodsId, table.goodsTypeId,
                        table.enabled, table.price, table.addTime, goods.name.as("goodsName"), goodsType.name.as("goodsTypeName"))
                .from(table)
                .leftJoin(goods).on(table.goodsId.eq(goods.id))
                .leftJoin(goodsType).on(table.goodsTypeId.eq(goodsType.id))
                .where(condition)
                .orderBy(table.enabled.desc(), table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public Page<WhPurchaseGoods> purchaseGoodsList(PageUtil pageUtil, String name, Long supplierId, Long categoryId, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(goods.name.like(name).or(goodsType.name.like(name)));
        if (categoryId != null) {
            condition.and(goods.categoryId.eq(categoryId));
        }
        if (supplierId != null) {
            condition.and(table.supplierId.eq(supplierId));
        }
        condition.and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.supplierId, table.price.as("supplierPrice"),
                        goods.categoryId, goods.name.as("goodsName"), goods.realStatus,
                        goodsType.id.as("goodsTypeId"), goodsType.name.as("goodsTypeName"),
                        goodsType.enabled, goodsType.price.as("unitPrice"), goodsType.img,
                        goodsType.unit, table.goodsId, goods.enabled.as("status"),
                        suppliers.name.as("supplierName"),
                        QueryMethods.ifNull(goodsWetPrice.usableNum, QueryMethods.number(0)).as("buyNum")
                )
                .from(table)
                .leftJoin(suppliers).on(table.supplierId.eq(suppliers.id))
                .leftJoin(goods).on(table.goodsId.eq(goods.id))
                .leftJoin(goodsType).on(table.goodsTypeId.eq(goodsType.id))
                .leftJoin(goodsWetPrice).on(table.goodsTypeId.eq(goodsWetPrice.goodsTypeId))
                .where(condition)
                .orderBy(table.supplierId.asc(), table.goodsId.asc(), table.goodsTypeId.asc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, WhPurchaseGoods.class);
    }

    @Override
    public long getDuplicate(Long supplierId, Long goodsId, Long goodsTypeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.supplierId.eq(supplierId))
                .and(table.goodsId.eq(goodsId))
                .and(table.goodsTypeId.eq(goodsTypeId));

        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public int dropData(Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public void updateEnabled(Long id, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id));
        UpdateChain.of(GoodsSuppliers.class)
                .set(table.enabled, enabled)
                .where(condition)
                .update();
    }

    @Override
    public void updateEnabledBySupplierId(Long supplierId, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.supplierId.eq(supplierId));
        UpdateChain.of(GoodsSuppliers.class)
                .set(table.enabled, enabled)
                .where(condition)
                .update();
    }

    @Override
    public int dropDataBySupplierId(Long supplierId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.supplierId.eq(supplierId));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean havesGoodsId(Long goodsId) {
        return exists(table.goodsId.eq(goodsId));
    }

    @Override
    public boolean havesGoodsTypeId(Long goodsTypeId) {
        return exists(table.goodsTypeId.eq(goodsTypeId));
    }

    @Override
    public List<GoodsSuppliers> getBySupplierIdAndGoodsTypeIdIn(Long supplierId, List<Long> goodsTypeIds) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.supplierId.eq(supplierId))
                .and(table.goodsTypeId.in(goodsTypeIds));
        return getMapper().selectListByCondition(condition);
    }

}
